﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Security.Cryptography;
using MS.Core.Exception;
using System.Data;
using System.Windows.Forms;
using MS.Core.Constants;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace LHS.Win
{
    public class Class
    {
        // Fields
        public static ArrayList arrGiaThuoc = new ArrayList();
        public static string isn;
        public static string mau = "Gray";
        private SymmetricAlgorithm mCryptoService = null;
        private ICryptoTransform mDecryptor = null;
        private ICryptoTransform mEncryptor = null;
        public static string name;

        // Methods
        public string Decrypt(string s)
        {
            this.mCryptoService = new DESCryptoServiceProvider();
            byte[] legalKey = this.GetLegalKey("CryptoLib");
            this.mCryptoService.Key = legalKey;
            this.mCryptoService.Mode = CipherMode.ECB;
            this.mDecryptor = this.mCryptoService.CreateDecryptor();
            string str = (s == null) ? string.Empty : s;
            if (str.Length > 0)
            {
                byte[] inputBuffer = Convert.FromBase64String(str);
                return Encoding.UTF8.GetString(this.mDecryptor.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }
            return str;
        }

        public string Encrypt(string s)
        {
            this.mCryptoService = new DESCryptoServiceProvider();
            byte[] legalKey = this.GetLegalKey("CryptoLib");
            this.mCryptoService.Key = legalKey;
            this.mCryptoService.Mode = CipherMode.ECB;
            this.mEncryptor = this.mCryptoService.CreateEncryptor();
            string str = (s == null) ? string.Empty : s;
            if (str.Length > 0)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(str);
                return Convert.ToBase64String(this.mEncryptor.TransformFinalBlock(bytes, 0, bytes.Length));
            }
            return str;
        }

        private byte[] GetLegalKey(string Key)
        {
            string s = Key;
            if (this.mCryptoService.LegalKeySizes.Length > 0)
            {
                int minSize = this.mCryptoService.LegalKeySizes[0].MinSize;
                if ((s.Length * 8) > this.mCryptoService.LegalKeySizes[0].MaxSize)
                {
                    s = s.Substring(0, this.mCryptoService.LegalKeySizes[0].MaxSize / 8);
                }
                else if ((s.Length * 8) < minSize)
                {
                    if (this.mCryptoService.LegalKeySizes[0].SkipSize == 0)
                    {
                        s = s.PadRight(minSize / 8, ' ');
                    }
                    else
                    {
                        while ((s.Length * 8) > minSize)
                        {
                            minSize += this.mCryptoService.LegalKeySizes[0].SkipSize;
                        }
                        s = s.PadRight(minSize / 8, ' ');
                    }
                }
            }
            return Encoding.ASCII.GetBytes(s);
        }

        /// <summary>
        /// Bắt lỗi hệ thống
        /// </summary>
        /// <param name="ex"></param>
        public static void ShowException(Exception ex)
        {
            ExceptionPolicy.HandleException(ex, "Global Policy");
        }

    }
}
